package com.lczyfz.campusinfo.modules.campusinfoeducation.web;

import com.google.common.collect.Lists;
import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.common.enums.OrderType;
import com.lczyfz.campusinfo.common.enums.PayType;
import com.lczyfz.campusinfo.common.utils.ApiResultUtils;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.modules.campusinfo.entity.CampusinfoSchool;
import com.lczyfz.campusinfo.modules.campusinfo.service.CampusinfoSchoolService;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoMajor;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoPubmed;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoMajorService;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoPubmedService;
import com.lczyfz.campusinfo.modules.campusinfoorder.entity.CampusinfoOrder;
import com.lczyfz.campusinfo.modules.campusinfoorder.service.CampusinfoOrderService;
import com.lczyfz.campusinfo.modules.campusinfopay.entity.CampusinfoPay;
import com.lczyfz.campusinfo.modules.campusinfopay.service.CampusinfoPayService;
import com.lczyfz.campusinfo.modules.sys.security.SystemAuthorizingRealm;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class CampusinfoApiMajor  extends BaseController {
    @Autowired
    private CampusinfoPayService campusinfoPayService;
    @Autowired
    private CampusinfoMajorService campusinfoMajorService;
    @Autowired
    private CampusinfoPubmedService campusinfoPubmedService;
    @Autowired
    private CampusinfoSchoolService campusinfoSchoolService;
    @Autowired
    private CampusinfoOrderService campusinfoOrderService;
    @RequestMapping(value = "api/campusinfoeducation/campusinfoMajor/majorList", method = RequestMethod.POST)
    @ResponseBody
    public List<Map<String, Object>> allType(String collegeid,HttpServletRequest request, HttpServletResponse response) {

        List<Map<String, Object>> mapList = Lists.newArrayList();
        CampusinfoMajor campusinfoMajor = new CampusinfoMajor();
        campusinfoMajor.setCollegeid(collegeid);
        List<CampusinfoMajor> campusinfoMajorList = campusinfoMajorService.findList(campusinfoMajor);
        for (int i = 0; i <  campusinfoMajorList.size(); i++) {
            Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
            CampusinfoMajor e =  campusinfoMajorList.get(i);
            map.put("id", e.getId());
            map.put("name", e.getMajorname());
            map.put("payment", e.getPayment());
            map.put("unlinepayment",e.getMajorcontent());
            mapList.add(map);
        }
        return mapList;
    }
    @RequestMapping(value = "api/campusinfoeducation/campusinfoMajor/addPubme", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addPubmed(CampusinfoPubmed campusinfoPubmed) {

        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        if (campusinfoPubmed != null) {
            SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
            campusinfoPubmed.setUserId(principal.toString());
            List<CampusinfoPubmed> campusinfoPubmeds = campusinfoPubmedService.findList(campusinfoPubmed);
            if(campusinfoPubmeds.isEmpty()){
                CampusinfoOrder campusinfoOrder = new CampusinfoOrder();
                campusinfoOrder.setSeqTag(OrderType.Major.getValue());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
                campusinfoOrderService.save(campusinfoOrder);
                String getmax = campusinfoOrderService.getmax();
                String Stringdata = sdf.format(new Date());
                campusinfoPubmed.setOrderCode(OrderType.Major.getValue()+Stringdata+getmax);
                CampusinfoPay campusinfoPay = new CampusinfoPay();
                campusinfoPay.setId(campusinfoPubmed.getOrderCode());
                campusinfoPay.setUser(UserUtils.getUser());
                campusinfoPay.setPaytype(PayType.ALIPAY.getValue());
                campusinfoPay.setStat(CampusinfoOrderStat.DONE.getValue());
                campusinfoPay.setGoodsName(OrderType.Major.getDesc());
                campusinfoPay.setAmount(campusinfoPubmed.getPayment());//费用
;               campusinfoPayService.ordersave(campusinfoPay);
                campusinfoPubmedService.save(campusinfoPubmed);
                map.put("message", "报名成功");
            } else {
                map.put("message","用户已经报名");
            }

        } else {
            map.put("message", "报名失败");
        }
        return map;
    }
    @RequestMapping(value = "api/campusinfoeducation/campusinfoMajor/addalipayPubme", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addepay(CampusinfoPubmed campusinfoPubmed) {

        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        if (campusinfoPubmed != null) {
            SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
            campusinfoPubmed.setUserId(principal.toString());
            List<CampusinfoPubmed> campusinfoPubmeds = campusinfoPubmedService.findList(campusinfoPubmed);
            if(campusinfoPubmeds.isEmpty()){
                CampusinfoOrder campusinfoOrder = new CampusinfoOrder();
                campusinfoOrder.setSeqTag(OrderType.Major.getValue());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
                campusinfoOrderService.save(campusinfoOrder);
                String getmax = campusinfoOrderService.getmax();
                String Stringdata = sdf.format(new Date());
                campusinfoPubmed.setOrderCode(OrderType.Major.getValue()+Stringdata+getmax);
                CampusinfoPay campusinfoPay = new CampusinfoPay();
                campusinfoPay.setId(campusinfoPubmed.getOrderCode());
                campusinfoPay.setUser(UserUtils.getUser());
                campusinfoPay.setPaytype(PayType.ALIPAY.getValue());
                campusinfoPay.setStat(CampusinfoOrderStat.DONE.getValue());
                campusinfoPay.setGoodsName(OrderType.Major.getDesc());
                campusinfoPay.setAmount(campusinfoPubmed.getPayment());//费用
                campusinfoPayService.ordersave(campusinfoPay);
                campusinfoPubmedService.save(campusinfoPubmed);
                map.put("message", "报名成功");
                map.put("campusinfoPubmeds",campusinfoPubmeds);
            } else if(campusinfoPubmeds.get(0).getStatus().equals("0")){
                map.put("campusinfoPubmeds",campusinfoPubmeds.get(0));
                map.put("message","用户已经报名但是未支付");
            } else {
                map.put("message","用户已经报名");
            }
        } else {
            map.put("message", "报名失败");
        }
        return map;
    }
    @RequestMapping(value = "api/campusinfo/campusinfoSchool/SchoolList", method = RequestMethod.POST)
    @ResponseBody
    public  List<Map<String, Object>>getschool() {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        CampusinfoSchool campusinfoSchool = new CampusinfoSchool();
        List<CampusinfoSchool> campusinfoSchools = campusinfoSchoolService.findList(campusinfoSchool);
        for (int i = 0; i <  campusinfoSchools.size(); i++) {
            Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
            CampusinfoSchool e =  campusinfoSchools.get(i);
            map.put("id", e.getId());
            map.put("name", e.getSchoolname());
            mapList.add(map);
        }
        return mapList;
    }
}
